package compro.admission.form.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import compro.admission.form.entity.Application;
import compro.admission.form.entity.User;
import compro.admission.form.service.ApplicationService;
import compro.admission.form.service.UserService;

@Controller
public class AdmissionController {
	
	private ApplicationService applicationService;
	private UserService userService;

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	@Resource
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}
	public UserService getUserService() {
		return userService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping("/searchApplicant")
	public String showsearch(Model model)
	{
		return "searchApplicant";
	}
	
	@RequestMapping(value="evaluateApplication", method=RequestMethod.GET)
	public String evaluateApplication(@RequestParam String app_id,HttpSession session) {
		long id = Long.parseLong(app_id);
		User staff = (User)userService.getUserAdmission();
		Application application = applicationService.getApplicationById(id);
		session.setAttribute("application", application);
		session.setAttribute("user", staff);
		return "evaluate_form";
	}
	
	@RequestMapping(value="resetApplication", method=RequestMethod.GET)
	public String resetApplication(@RequestParam String app_id,HttpSession session) {
		long id = Long.parseLong(app_id);
		User staff = (User)userService.getUserAdmission();
		Application application = applicationService.getApplicationById(id);
		session.setAttribute("application", application);
		session.setAttribute("user", staff);
		return "reset_application";
	}

	@RequestMapping(value="resetApprove", method=RequestMethod.POST)
	public String resetApprove(HttpSession session,ModelMap model) {
		Application application = (Application)session.getAttribute("application");
		applicationService.resetApplication(true, application);
		model.addAttribute("title", "Request to Reset Application.");
		model.addAttribute("msg","You decision is sent to application successfully.");
		return "success";
	}
	@RequestMapping(value="rejectApplication", method=RequestMethod.POST)
	public String rejectApplication(HttpSession session,ModelMap model) {
		Application application = (Application)session.getAttribute("application");
		applicationService.passFailApplication(application,false);
		model.addAttribute("title", "Request to Reject Application.");
		model.addAttribute("msg","You decision is sent to application successfully.");
		return "success";
	}
	@RequestMapping(value="approveApplication", method=RequestMethod.POST)
	public String approveApplication(HttpSession session,ModelMap model) {
		Application application = (Application)session.getAttribute("application");
		applicationService.passFailApplication(application,true);
		model.addAttribute("title", "Request to Approve Application.");
		model.addAttribute("msg","You decision is sent to application successfully.");
		return "success";
	}
	@RequestMapping(value="resetReject", method=RequestMethod.POST)
	public String resetReject(HttpSession session,ModelMap model) {
		Application application = (Application)session.getAttribute("application");
		applicationService.passFailApplication(application,false);
		model.addAttribute("title", "Request to Reset Application.");
		model.addAttribute("msg","You decision is sent to application successfully.");
		return "success";
	}
	
	@RequestMapping("/showAdmissionForm")
	public String showAdmissionForm(Model model)
	{
		return "admission_Form";
	}
	
	@RequestMapping("/showAdmissionSearchForm")
	public String showSearch(Model model)
	{
		return "admission_searchform";
	}
	
	@RequestMapping(value = "/showSearchApplicationID")
	public String showSearchApplicationID(Model model)
	{
		return "searchApplicant";
	}
	
	@RequestMapping(value="/searchByEmail", method=RequestMethod.POST)
	public String SearchApplicationbyemail(String email,HttpSession session) {
		User user = (User)userService.getUserbyemail(email);
		Application application=(Application)applicationService.getApplicationByUser(user);
		session.setAttribute("application", application);
		return "searchresult";
		
	}
	@RequestMapping (value="/searchById", method=RequestMethod.POST)
	public String SearchApplicationbyid(long id, HttpSession session) {
		Application application = (Application)applicationService.getApplicationById(id);		
		session.setAttribute("application", application);
		return "searchresult";
	}
}

